require("./index.scss");
var ParseTemplate = ZFT.Util.ParseTemplate;
var Ajax = ZFT.Util.Ajax;
var STip = ZFT.Util.STip;
var Pagination = require("COMMON/modules/pagination-x/v1.0");
var Template = {
    tr: ParseTemplate(require("./tpl/tr.xtpl")),
    modal: ParseTemplate(require("./tpl/modal.xtpl"))
}

var Main = ZFT.Util.Class({
    container: $("body"),
    init: function () {
        var _this = this;
        _this.initDatepicker();
        _this.getLogsList(1);
        var pagination = this.pagination = new Pagination({
            container: $(".paginationWrap"),
            count: 5,
            showTotal: true,
            jump: true,
            filter: true
        });
        pagination.on("page.switch", function (toPage, currentPage, totalPage) {
            pagination.render({ current: currentPage, toPage: toPage }),
                _this.getLogsList(toPage);
        });

    },
    EVENTS: {
        "click #getLogs": "getLogsList",
        "keyup #keyInp": "toGetLogList",
        "click #sendMsgBtn":"reSendMsg"
    },
    initDatepicker: function () {
        $("#beginDate").datepicker({
            format: "yyyy-mm-dd",
            maxViewMode: 2,
            language: "zh-CN",
            autoclose: true
        });
        $('#endDate').datepicker({
            format: "yyyy-mm-dd",
            maxViewMode: 2,
            language: "zh-CN",
            autoclose: true
        });
    },
    getLogsList: function (toPage) {
        var _this = this,
            beginDate = $("#beginDate").val(),
            endDate = $("#endDate").val(),
            notify_method = $("#notifyMethod").val(),
            event_type = $("#eventType").val(),
            keyType = $("#keyType").val(),
            keyVal = $.trim($("#keyInp").val()),
            tbody = $("#tbody"),
            pagin = $(".paginationWrap"),
            pageSize = pagin.attr("data-size"),
            envTypeArr={1:'催缴',2:'到账'};
            if(typeof (toPage) != "number") toPage=1;
        param = {
            mobile: (keyType == 1) ? keyVal : '',
            room: (keyType == 2) ? keyVal : '',
            begin: _this.translateTimeToTimeStamp(beginDate),
            end: _this.translateTimeToTimeStamp(endDate),
            notify_method: notify_method,
            event_type: event_type,
            page:toPage,
            page_size: pageSize ? pageSize : 10,
        }
        
        if (parseInt(param['end']) < parseInt(param['begin'])) return STip("fail", "开始时间不能大于截止时间！");
        ZFT.Util.Ajax("/r/PayNoticeList_getSendList", {
            type: "POST",
            dataType: "json",
            params: param,
            loading: function (res) {
                var loading = '<tr><td colspan="6">加载中，请稍后...</td></tr>';
                pagin.hide();
                tbody.html(loading);
            },
            success: function (res) {
                if (res.code == 200) {
                    var list = res.data.list,
                        html,
                        sendtime;
                    sendtime=_this.translateTimeStampToTime()
                    for(var i=0,len=list.length;i<len;i++){
                        list[i]['sendtime']=_this.translateTimeStampToTime(list[i].send_time);
                        list[i]['eventype']=envTypeArr[event_type];
                    }
                    html = Template.tr({ data: list });
                    tbody.html(html);
                    _this.pagination.render({ current: toPage, toPage: toPage, total: res.data.total });
                } else {
                    var err = '<tr><td colspan="6">' + res.msg + '</td></tr>';
                    tbody.html(err);
                }
            }
        });
    },
    reSendMsg:function(){
        var _this=this,
            html,
            tbody=$("#tbody"),
            msg=$("#tbody .u-inp_msg:checked"),
            modal=$("#modal")
            msgVal=msg.val(),
            arr=[];
        if(!msgVal) return STip("fail","请至少选中一个短信模板！");
        for(var i=0,len=msg.length;i<len;i++){
            arr.push(msg.eq(i).val());
        }
        html=Template.modal();
        modal.html(html);
        $("#confBtn").on("click",function(e){
            var _this=this,
                tarBtn=$(e.currentTarget);
            ZFT.Util.Ajax("/r/PayNoticeList_reSend",{
                type:"POST",
                dataType:"json",
                params:{
                    id_arr:JSON.stringify(arr)
                },
                loading:function(){
                    tarBtn.attr("disabled",true).removeClass("m-btn_primary").text("发送中...");
                },
                success:function(res){
                    if(res.code==200){
                        modal.html("");
                        STip("success",res.msg);
                    }else{
                        STip("fail",res.msg);
                        modal.html("");
                    }
                }
            })
        });
        
    },
    translateTimeToTimeStamp: function (time) {
        var date = new Date(time);
        var timestamp = date.getTime() / 1000;
        if (!time) return timestamp = '';
        timestamp = date.getTime() / 1000;
        return timestamp;

    },
    translateTimeStampToTime: function (unixtime) {
        // var date = new Date(parseInt(time) * 1000).toLocaleString(time).replace(/:\d{1,2}$/, ' ');
        // var date = new Date(parseInt(time) * 1000).toLocaleString(time);
        var time = new Date(unixtime * 1000);
        var year = time.getFullYear();
        var month = time.getMonth() + 1;
        var date = time.getDate();
        var dateTime = year + "-" + month + "-" + date;
        return dateTime;
    },
    toGetLogList: function (e) {
        var _this = this;
        var keycode = e.keyCode;
        if (keycode !== 13) return false;
        _this.getLogsList(1);
    },
});

$(function () {
    new Main();
})